'''
Created on 27.03.2009

@author: kristina
'''
import ConfigParser
import  re
from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy import sql
    
config = ConfigParser.RawConfigParser()
config.read('../../setup.cfg')

SERVER = config.get('Engine', 'server')
DB = config.get('Db_MediaWiki','db')
USER = config.get('Db_MediaWiki', 'user')
PASS = config.get('Db_MediaWiki', 'password')

CHUNK_SIZE = 100

wiki_url = 'mysql://' + USER + ':' + PASS +'@' + SERVER +'/' + DB
wiki_engine = create_engine(wiki_url)
wiki_meta = MetaData(wiki_engine)
wiki_meta.create_all(wiki_engine)

revision_table = Table('revision',wiki_meta,autoload=True)
page_table = Table('page',wiki_meta,autoload=True)
text_table = Table('text',wiki_meta,autoload=True)


def filter(keyword, new_db_name):

    filtered_wiki_url = 'mysql://' + USER + ':' + PASS +'@' + SERVER +'/' + new_db_name
    filtered_wiki_engine = create_engine(filtered_wiki_url)
    filtered_wiki_meta = MetaData(filtered_wiki_engine)
    
    filtered_table = Table('wiki',filtered_wiki_meta,
        Column('page_id',Integer, primary_key = True,autoincrement = True),
        Column('title', String(255)),
        Column('text', BLOB)
    )
    filtered_wiki_meta.create_all(filtered_wiki_engine)
    
    
    result = select([func.count(revision_table.c.rev_id).label("count")]).execute()
    row = result.fetchone()
    total_size = row["count"]
    print "Total size %d" % total_size
    
    for i in range(0, total_size, CHUNK_SIZE):
        result = select([revision_table, page_table,text_table], 
                        and_(revision_table.c.rev_page == page_table.c.page_id,
                             revision_table.c.rev_text_id == text_table.c.old_id),
                             limit = CHUNK_SIZE, offset = i
                        ).execute()
               
        rows = result.fetchall()
        for row in rows:
            text = row['old_text']
            m = re.search(keyword, text, re.IGNORECASE)
            if (m != None):
                filtered_table.insert().execute(title = row['page_title'], text = row['old_text'])
        print "%d entries filtered" % (i  CHUNK_SIZE) 
        result.close()
    
filter('computer','computer_wiki')  